﻿@page "/chntable"
@attribute [TabItemOption(Text = "通道列表")]
@using BootstrapBlazor.Components
@using TrackSystem.Entities.Dtos.Channel;
@using TrackSystem.Entities.Extensions


<PageTitle>通道列表</PageTitle>

<Table TItem="ChannelWithSensorsDto"
       IsPagination="true" PageItemsSource="@PageItemsSource"
       IsStriped="true" IsBordered="true" IsMultipleSelect="false" EditDialogIsDraggable="true"
       ShowToolbar="true" ShowExtendButtons="true"
       ShowSkeleton="true"
       IsExtendButtonsInRowHeader="true" ShowSearch="true"
       OnQueryAsync="@OnQueryAsync"
       OnSaveAsync="@OnSaveAsync"
       OnDeleteAsync="@OnDeleteAsync">

    <TableColumns>

        <TableColumn @bind-Field="@context.Id" Sortable="true" Width="50" />
        <TableColumn @bind-Field="@context.Name" Sortable="true" Width="200">
            <Template Context="value">
                <div class="d-flex">
                    @{
                        ChannelDto row = value.Row;
                    }
                    <div class="ps-2">
                        <div>@value.Value</div>
                        <div class="user-demo-address">@row.Description</div>
                    </div>
                </div>
            </Template>
        </TableColumn>
        <TableColumn @bind-Field="@context.PortCategory" Align="Alignment.Center">
            <Template Context="value">
                <div class="ps-2">
                    <div>@value.Value</div>
                    <div>
                        @{
                            ChannelDto row = value.Row;
                            string msg = row.PortCategory switch
                            {
                                PortCategory.UDP => $"远程:{row.Ip}:{row.Port},本地:{row.localIp}:{row.localPort}",
                                PortCategory.SRIAL => $"{row.COMn},{row.Baudrate},{row.SerialParams}",
                                _ => $"{row.Ip}:{row.Port}"
                            };
                        }
                        @msg
                    </div>
                </div>
            </Template>
        </TableColumn>

        <TableColumn @bind-Field="@context.Protocol" Align="Alignment.Center" Width="300">
            <Template Context="value">
                <div class="ps-2">
                    <div>@value.Value</div>
                    <div>
                        @{
                            ChannelDto row = value.Row;
                            if (row.Protocol == ProtocolEnum.Modbus || row.Protocol == ProtocolEnum.ModbusTCP)
                            {
                                <div class="row">
                                    <div class="col-6">
                                        <Switch IsDisabled="true" Value="@row.optSplit3_4Cmd" OnColor="Color.Success" OnText="34合并" OffText="34分离"></Switch>
                                    </div>
                                    <div class="col-6">
                                        <Switch IsDisabled="true" Value="@row.optSupport21Cmd" OnColor="Color.Success" OnText="同步读写" OffText="异步读写"></Switch>
                                    </div>
                                </div>
                            }
                        }
                    </div>
                </div>
            </Template>
        </TableColumn>

        <TableColumn @bind-Field="@context.Interval" Align="Alignment.Left">
            <Template Context="value">
                <div>@value.Value ms</div>
            </Template>
        </TableColumn>

        <TableColumn @bind-Field="@context.Sensors" Align="Alignment.Center">
                <Template Context="value">
                    <div class="ps-2">
                        <div>
                        @if (value.Row is { Sensors.Count: 0 })
                        {
                            <span class="badge bg-secondary">无传感器</span>
                        }
                        </div>
                        <div>
                        @foreach (var sensor in value.Row.Sensors)
                        {
                            <Button Text="@sensor.Name"
                                    ButtonStyle="ButtonStyle.Round"
                                    IsOutline="true"
                                    onclick="@(() => SkipToSensor(sensor.Id))"
                                    Color="Color.Primary">
                            </Button>
                        }
                        </div>
                    </div>
                </Template>
            </TableColumn>
    </TableColumns>


    <EditTemplate>
        <div class="row g-3 form-inline">
            <div class="col-12 col-sm-6">
                <BootstrapInput @bind-Value="@context.Name"  maxlength="20"></BootstrapInput>
            </div>
            <div class="col-12 col-sm-6">
                <BootstrapInput @bind-Value="@context.Description" maxlength="50"></BootstrapInput>
            </div>
            <div class="col-12 col-sm-12">
                <Dropdown @bind-Value="@context.PortCategory" DataSource="@_lstPortCategory" OnSelectedItemChanged="@OnCategoryChanged" />
            </div>

            <div class="col-12 col-sm-6" >
                <IpAddress @bind-Value="@context.Ip" />
            </div>
            <div class="col-12 col-sm-6" id="tt2">
                <BootstrapInputNumber @bind-Value="@context.Port"/>
            </div>
            <div class="col-12 col-sm-6">
                <IpAddress @bind-Value="@context.localIp" />
            </div>
            <div class="col-12 col-sm-6">
                <BootstrapInputNumber @bind-Value="@context.localPort" />
            </div>

            <div class="col-12 col-sm-6">
                <BootstrapInput @bind-Value="@context.COMn" />
            </div>
            <div class="col-12 col-sm-6">
                <BootstrapInputNumber @bind-Value="@context.Baudrate" />
            </div>
            <div class="col-12 col-sm-6">
                <BootstrapInput @bind-Value="@context.SerialParams" />
            </div>


            <div class="col-12 col-sm-12">
                <Dropdown @bind-Value="@context.Protocol" DataSource="@_lstProtocol" />
            </div>

            @if (context.Protocol == ProtocolEnum.Modbus || context.Protocol == ProtocolEnum.ModbusTCP)
            {
                <div class="col-12 col-sm-6">
                    <Switch @bind-value="@context.optSplit3_4Cmd"  OnColor="Color.Success" OnText="34合并" OffText="34分离"></Switch>
                </div>

                <div class="col-12 col-sm-6">
                    <Switch @bind-value="@context.optSupport21Cmd" OnColor="Color.Success" OnText="读写合并" OffText="读写分离"></Switch>
                </div>
            }

            <div class="col-12 col-sm-6">
                <BootstrapInputNumber @bind-Value="@context.Interval" />
            </div>

        </div>
    </EditTemplate>

</Table>

